<template>
    <van-nav-bar fixed style="background-color: black;">
        <template #right>
            <span style="color: #fff;margin-left: -230px;">短信验证码登录</span>
        </template>
    </van-nav-bar>
    <div style="height: 50px;"> </div>
    <div style="width: 100%;height: 800px;background-color: #eeeeee;padding:  20px 0;">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="fieldValue" is-link readonly placeholder="国家和地区" @click="showPicker = true" />
                <van-popup v-model:show="showPicker" destroy-on-close round position="bottom">
                    <van-picker :model-value="pickerValue" :columns="columns" @cancel="showPicker = false"
                        @confirm="onConfirm" />
                </van-popup>
                <van-field v-model="username" name="username" placeholder="手机号"
                    :rules="[{ pattern, message: '请填写手机号' }]" />
                <van-field v-model="sms" center clearable placeholder="短信验证码"
                    :rules="[{ required: true, message: '请填写密码' }]">
                    <template #button>
                        <van-button size="small" type="primary" @click="yzm">发送验证码</van-button>
                    </template>
                </van-field>
            </van-cell-group>

            <div style="margin: 16px;">
                <van-button block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
        </van-form>
<img src="../img/登录_03.jpg" alt="" style="width: 100%;">
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { loginApi } from '@/api/api';
const username = ref('');

import { useRouter } from 'vue-router';
const router = useRouter()

const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value).then(res => {
        console.log(res);
        if (res.data.code == 0) {
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('username', res.data.data.username)
            router.push('/home/my')
        }
    })
};
const sms = ref('')
const yzm = () => {
    sms.value = Math.floor(Math.random() * 1000000)
}
const columns = [
    { text: '中国', value: 'Hangzhou' },
    { text: '美国', value: 'Ningbo' },
    { text: '日本', value: 'Wenzhou' },
    { text: '英国', value: 'Shaoxing' },
    { text: '俄罗斯', value: 'Huzhou' },
];
const fieldValue = ref('');
const showPicker = ref(false);
const pickerValue = ref([]);
const onConfirm = ({ selectedValues, selectedOptions }) => {
    showPicker.value = false;
    pickerValue.value = selectedValues;
    fieldValue.value = selectedOptions[0].text;
};
const pattern=/^1[1-9]\d{9}/


</script>

<style lang="scss" scoped></style>